package cn.iocoder.yudao.module.erp.controller.admin.dp.dpnj;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpnj.vo.DpNjPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpnj.vo.DpNjRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpnj.vo.DpNjSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpNjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemNjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpEastListNjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpWestListNjDO;
import cn.iocoder.yudao.module.erp.service.dp.DpNjService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - ERP南疆口岸仓库制单")
@RestController
@RequestMapping("/erp/dp-nj")
@Validated
public class DpNjController {

    @Resource
    private DpNjService dpNjService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP南疆口岸仓库制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:create')")
    public CommonResult<String> createDpNj(@Valid @RequestBody DpNjSaveReqVO createReqVO) {
        return success(dpNjService.createDpNj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP南疆口岸仓库制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:update')")
    public CommonResult<Boolean> updateDpNj(@Valid @RequestBody DpNjSaveReqVO updateReqVO) {
        dpNjService.updateDpNj(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP南疆口岸仓库制单")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:delete')")
    public CommonResult<Boolean> deleteDpNj(@RequestParam("id") String id) {
        dpNjService.deleteDpNj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP南疆口岸仓库制单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:query')")
    public CommonResult<DpNjRespVO> getDpNj(@RequestParam("id") String id) {
        DpNjDO dpNj = dpNjService.getDpNj(id);
        return success(BeanUtils.toBean(dpNj, DpNjRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP南疆口岸仓库制单分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:query')")
    public CommonResult<PageResult<DpNjRespVO>> getDpNjPage(@Valid DpNjPageReqVO pageReqVO) {
        PageResult<DpNjDO> pageResult = dpNjService.getDpNjPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpNjRespVO.class));
    }

    @GetMapping("/dpNjOuterList")
    @Operation(summary = "获得ERP南疆制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:outer')")
    public CommonResult<PageResult<DpNjRespVO>> dpNjOuterList(@Valid DpNjPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpNjDO> dpNjOuterList = dpNjService.dpNjOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpNjOuterList, DpNjRespVO.class));
    }

    @GetMapping("/dpNjVehicleList")
    @Operation(summary = "获得ERP南疆配车列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:outer')")
    public CommonResult<PageResult<DpNjRespVO>> dpNjVehicleList(@Valid DpNjPageReqVO pageReqVO) {
        PageResult<DpNjDO> dpNjOuterList = dpNjService.dpNjOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpNjOuterList, DpNjRespVO.class));
    }

    @PostMapping("/dpNjOutStroe")
    @Operation(summary = "ERP南疆制单配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:outer')")
    public CommonResult<Boolean> dpNjOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpNjService.dpNjOutStroe(createReqVO);
        return success(true);
    }

    @GetMapping("/dpNjEnterList")
    @Operation(summary = "获得ERP南疆制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpYwEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                   @RequestParam(value = "line",required = false) Integer line,
                                                                   @RequestParam(value = "dest",required = false) Integer dest,
                                                                   @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpNjEnterList = dpNjService.dpNjEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpNjEnterList, DispatchVehicleRespVO.class));
    }


    @PostMapping("/enterStroeDpNj")
    @Operation(summary = "ERP南疆运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:enter')")
    public CommonResult<Boolean> enterStroeDpNj(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpNjService.enterStroeDpNj(createReqVO);
        return success(true);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP南疆制单 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpNjExcel(@Valid DpNjPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpNjDO> list = dpNjService.getDpNjPage(pageReqVO).getList();
        // 对数据进行字段处理
        List<DpNjRespVO> processedList = list.stream().map(dpNjDO -> {
            DpNjRespVO respVO = BeanUtils.toBean(dpNjDO, DpNjRespVO.class);
            // 格式化创建时间
//            if (dpNjDO.getCreateTime() != null) {
//                respVO.setCreateTimeFormatted(dpNjDO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            }
            // 转换核销字段
            if (dpNjDO.getWriteOff() != null){
                respVO.setWriteOffFormatted(dpNjDO.getWriteOff() == 1 ? "是" : "否");
            }
            // 转换线路字段
            if (dpNjDO.getLine() != null) {
                respVO.setLineFormatted(dpNjDO.getLine() == 1 ? "南疆" : "西线");
            }
            return respVO;
        }).collect(Collectors.toList());
        processedList.add(new DpNjRespVO());
        processedList.add(new DpNjRespVO());
        // 创建提示信息行，并将其添加到列表末尾
        DpNjRespVO footerRow = new DpNjRespVO();
        footerRow.setId("注意：" +
                "一：提货时双方当面交接清楚，如货物提走后发现损坏少货现象本公司不负责。\n" +
                "二：预期不提货者，产生一切费用及后果由客户自己承担。\n" +
                "三：易碎的货物在承运过程中发现破碎、丢失时，只赔偿丢失的货物，不赔偿破损的货物。\n" +
                "义乌库房地址:浙江省义乌市苏溪高君路7号(浙江博尼锦纶服份有限公司西)大门进去直走100米311库房 电话/Tel:15600000311(微信同号)\n" +
                "广州库房地址:广东省佛山市南海区里水镇河塱沙里水东部工业园10号南海毛中厂二期(东侧大院311库房) 电话/Te1:18500000311(微信同号)\n" +
                "新疆库房地址:新疆维吾尔自治区乌鲁木齐市沙依巴克区青葱路1048号附1号楼311库 电话/Te1:18199990311(微信同号)");
        // 将提示信息添加到列表末尾
        processedList.add(footerRow);
        // 导出 Excel
        ExcelUtils.write(response, "ERP南疆制单.xls", "数据", DpNjRespVO.class, processedList);
//                        BeanUtils.toBean(processedList, DpYwRespVO.class));
    }

    @GetMapping("/export-list")
    @Operation(summary = "导出ERP南疆制单东西线清单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:exportlist')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpNjExcelList(@Valid DpNjDO dpNjDO,HttpServletResponse response) throws IOException {
        //获取线路类型
        Integer  line = dpNjDO.getLine();
        List<DpEastListNjDO> dpEastList = dpNjService.getDpEastListNjListByNo(dpNjDO.getNo());
        List<DpWestListNjDO> dpWestList = dpNjService.getDpWestListNjListByNo(dpNjDO.getNo());
        if (line.equals(1)){
            // 导出 Excel南疆清单
            ExcelUtils.write(response, "ERP南疆制单南疆清单.xls", "清单",DpEastListNjDO.class,
                    BeanUtils.toBean(dpEastList,DpEastListNjDO.class));
        }
        if (line.equals(2)){
            // 导出 Excel西线清单
            ExcelUtils.write(response, "ERP南疆制单西线清单.xls", "清单",DpWestListNjDO.class,
                    BeanUtils.toBean(dpWestList,DpWestListNjDO.class));
        }
    }


    @PutMapping("/examine")
    @Operation(summary = "ERP物流南疆制单审核")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:examine')")
    public CommonResult<Boolean> examineDpNj(@Valid @RequestBody DpNjDO dpNjDO){
        dpNjService.examineDpNj(dpNjDO);
        return success(true);
    }

    // ==================== 子表（ERP南疆制单货号详情） ====================

    @GetMapping("/dp-item-nj/list-by-no")
    @Operation(summary = "获得ERP南疆制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:detail')")
    public CommonResult<List<DpItemNjDO>> getDpItemNjListByNo(@RequestParam("no") String no) {
        return success(dpNjService.getDpItemNjListByNo(no));
    }

    // ==================== 子表（南疆南疆清单） ====================

    @GetMapping("/dp-east-list-nj/list-by-no")
    @Operation(summary = "获得南疆南疆清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:detail')")
    public CommonResult<List<DpEastListNjDO>> getDpEastListNjListByNo(@RequestParam("no") String no) {
        return success(dpNjService.getDpEastListNjListByNo(no));
    }

    // ==================== 子表（南疆西线清单） ====================

    @GetMapping("/dp-west-list-nj/list-by-no")
    @Operation(summary = "获得南疆西线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-nj:detail')")
    public CommonResult<List<DpWestListNjDO>> getDpWestListNjListByNo(@RequestParam("no") String no) {
        return success(dpNjService.getDpWestListNjListByNo(no));
    }


}